package com.wangling.service;

import com.wangling.dao.UserDao;
import com.wangling.model.User;
import org.mindrot.jbcrypt.BCrypt;

public class UserService {
    private final UserDao userDao = new UserDao();
    public User register(String username, String nickname, String avatar, String git, String password) {
        String salt = BCrypt.gensalt();
        String hashpw = BCrypt.hashpw(password, salt);

        return userDao.insert(username, nickname, avatar, git, hashpw);
    }

    public User login(String username, String password) {
        User user = userDao.selectOneByUsername(username);

        if (user != null) {
            if (BCrypt.checkpw(password, user.password)) {
                return user;
            }
        }
        return null;
    }
}
